package com.hzit.hzb.core.controller.admin;


import com.hzit.common.exception.Assert;
import com.hzit.common.exception.BusinessException;
import com.hzit.common.result.R;
import com.hzit.common.result.ResponseEnum;
import com.hzit.hzb.core.pojo.entity.IntegralGrade;
import com.hzit.hzb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author hbxfwf
 * @since 2024-07-17
 */
@Api(tags = "积分等级接口")
@RestController
@RequestMapping("/admin/core/integralGrade")
@Slf4j
// @CrossOrigin
public class AdminIntegralGradeController {

	@Autowired
	private IntegralGradeService integralGradeService;

	@ApiOperation(value = "列表积分等级",notes = "查看各个等级")
	@GetMapping("/list")
	public R listAll(){
		log.info("hi i'm helen");
		log.warn("warning!!!");
		log.error("it's a error");
		List<IntegralGrade> list = integralGradeService.list();
		return R.ok().data("list",list);
	}

	@ApiOperation(value = "删除指定的积分等级")
	@DeleteMapping("/remove/{id}")
	public R removeById(
			@ApiParam(value = "积级等级id")
			@PathVariable Long id){
		boolean b = integralGradeService.removeById(id);
		if(b){
			return R.ok().message("删除积分等级成功！");
		}
		return R.error().message("删除积分等级失败！");
	}

	@ApiOperation("新增积分等级")
	@PostMapping("/save")
	public R save(
			@ApiParam(value = "积分等级对象", required = true)
			@RequestBody IntegralGrade integralGrade){
		if(integralGrade.getBorrowAmount() == null){
			// throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
			// 用断言代替
			Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
		}

		boolean result = integralGradeService.save(integralGrade);
		if (result) {
			return R.ok().message("保存成功");
		} else {
			return R.error().message("保存失败");
		}
	}

	@ApiOperation("根据id获取积分等级")
	@GetMapping("/get/{id}")
	public R getById(
			@ApiParam(value = "数据id", required = true, example = "1")
			@PathVariable Long id
	){
		IntegralGrade integralGrade = integralGradeService.getById(id);
		if(integralGrade != null){
			return R.ok().data("record", integralGrade);
		}else{
			return R.error().message("数据不存在");
		}
	}

	@ApiOperation("更新积分等级")
	@PutMapping("/update")
	public R updateById(
			@ApiParam(value = "积分等级对象", required = true)
			@RequestBody IntegralGrade integralGrade){
		boolean result = integralGradeService.updateById(integralGrade);
		if(result){
			return R.ok().message("修改成功");
		}else{
			return R.error().message("修改失败");
		}
	}
}

